package com.ibiz.znjd;

import android.os.Bundle;
import android.webkit.CookieManager;
import android.webkit.WebView;

import com.baidu.location.BDLocation;
import com.getcapacitor.BridgeActivity;
import com.getcapacitor.JSObject;
import com.getcapacitor.Logger;
import com.getcapacitor.Plugin;
import com.ibiz.znjd.baidu.location.LocationService;
import com.ibiz.znjd.baidu.location.LocationServiceListener;
import com.ibiz.znjd.baidu.location.NotificationUtils;
import com.ibiz.znjd.plugin.CommonPlugin;
import com.ibiz.znjd.plugin.FacePlugin;
import com.ibiz.znjd.plugin.LocationPlugin;
import com.ibiz.znjd.utils.KeepAliveUtil;
import com.ibiz.znjd.znjd.GjInternal;
import com.ibiz.znjd.znjd.RecordPlanUtil;
import com.ibiz.znjd.znjd.WaringUtil;

import java.util.ArrayList;
import java.util.Date;

public class MainActivity extends BridgeActivity {
    private LocationService locationService;
    public NotificationUtils notificationUtils;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 初始化通知
        notificationUtils = new NotificationUtils(MainActivity.this);

        // 启动location服务
        locationService = ((MainApplication) getApplicationContext()).getLocationService();
        locationService.setLocationOption(locationService.getDefaultLocationClientOption());
        locationService.registerListener(locationServiceListener);
        locationService.start(MainActivity.this);

        // 保活
        KeepAliveUtil.ignoreBatteryOptimization(MainActivity.this);
//    try{
//      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//        Intent intent = new Intent(this, ForegroundService.class);
//        startForegroundService(intent);
//      }
//    }catch(Exception e){
//      e.printStackTrace();
//    }

        // Initializes the Bridge
        this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
            // Additional plugins you've installed go here
            add(LocationPlugin.class);
            add(CommonPlugin.class);
            add(FacePlugin.class);
        }});
    }

    @Override
    public void onPause() {
        super.onPause();
        // 保证cookie在关闭后也能使用
        CookieManager.getInstance().flush();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        locationService.stop();
        if(null != ((MainApplication) getApplicationContext()).geoFenceService){
            ((MainApplication) getApplicationContext()).geoFenceService.removeGeoFence();
        }
    }

    /**
     * 校验定位的信息，经纬度，地址是否有值。
     * 如果有问题，则返回true
     *
     * @param locationObj
     * @return
     */
    private Boolean validateLocation(JSObject locationObj) {
        try {
            return !locationObj.getBoolean("isSuccess") ||
                    locationObj.getDouble("longitude") == 0 ||
                    (Object) locationObj.getDouble("longitude") == null ||
                    "".equals((Object) locationObj.getDouble("longitude")) ||
                    locationObj.getDouble("latitude") == 0 ||
                    (Object) locationObj.getDouble("latitude") == null ||
                    "".equals((Object) locationObj.getDouble("latitude")) ||
                    "".equals(locationObj.getString("addr")) ||
                    locationObj.getString("addr") == null;
        } catch (Exception e) {
            return true;
        }
    }

    /**
     * 定位服务监听器
     */
    private LocationServiceListener locationServiceListener = new LocationServiceListener() {
        @Override
        public void onReceiveLocation(BDLocation var1, JSObject locationObj) {
            RecordPlanUtil.test(MainActivity.this);
            WaringUtil.updateCurrentTime(((MainApplication) getApplicationContext()));
            long current = new Date().getTime();
            long timeInterval = 1000 * 60 * 10; // 10分钟
            long lastRecordTime = ((MainApplication) getApplication()).lastRecordTime;
            if (lastRecordTime == 0 || (lastRecordTime != -1 && (current - lastRecordTime) > timeInterval)) {
                // 如果定位没有经纬度的话，取消该次上传，10秒后再继续
                if (validateLocation(locationObj)) {
                    ((MainApplication) getApplication()).lastRecordTime += 1000 * 10;
                    return;
                }
                GjInternal.send((MainApplication) getApplication());
                ((MainApplication) getApplication()).lastRecordTime = current;
            }
        }
    };

}
